
/*
	@title   网络请求封装  
	@time    2021-07-17
	@author  付天博
*/
export default{
	common:{
        baseUrl:'https://tzdj.1qit.com/',
        header:{
            'Content-Type' : 'application/json;charset=UTF-8',
            "Content-Type": "application/x-www-form-urlencoded",
        },
        data:{},
        method:'POST',
        dataType:'json'
    },
	request(options ={}){
		options.url = this.common.baseUrl + options.url
		// options.header = options.header || this.common.header
		options.header = {
			'Content-Type' : 'application/json;charset=UTF-8',
			"Content-Type": "application/x-www-form-urlencoded",
			'token':uni.getStorageSync("token")
		}
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		return new Promise((res,rej)=>{
			uni.request({
				...options,
				success: (result) => {
					if(result.data.code === 401){
						uni.showToast({
							title:"登陆过期，请重新登录",
							icon:"none",
							duration:1500
						})
						setTimeout(()=>{
							uni.navigateTo({
								url:"/pages/login/index"
							})
						},1500)
					}
                    res(result)
				},
				fail: (error) => {
					console.log(error)
				}
			});
		})
	},
	post(url,data={},options={}){
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	}
}